<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Management extends MX_Controller {
    
  function __construct() {
    $this->menu = $this->cek();
  }
	
	public function index($folder, $style){
    if($style == 1){
      $set_folder = array(
          "file_current"    => $this->session->userdata("file_current")."/".$folder,
          "folder_current"  => $folder
      );
    }
    elseif($style == 2){
      $st = substr($this->session->userdata("file_current"), 0, (-1 * (strlen($folder) + 1)));
//      print $this->session->userdata("file_current")."<br />";
//      print strlen($folder)."|";
//      print $st;die;
      $e_st = explode("/", $st);
      $set_folder = array(
          "file_current"    => $st,
          "folder_current"  => end($e_st)
      );
    }
    elseif($style == 3){
      $set_folder = array(
          "file_current"    => "",
          "folder_current"    => ""
      );
    }
    if($folder){
      $this->session->set_userdata($set_folder);
      redirect("file/management/index");
    }
    
    $files = scandir('./files/'.trim($this->session->userdata("file_current"),"/"));
    $this->template->build("management/management",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'file/management',
            'title'       => lang("file_management"),
            'files'       => $files
          ));
    $this->template
      ->set_layout('default')
      ->build("management/management");
	}
	public function add_topic($nicename){
    if(!$nicename)
      redirect("forum/add-topic/create");
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"             => $pst['title'],
            "nicename"          => $this->global_models->nicename($pst['title']),
            "note"              => str_replace("../../../", base_url(), $pst['note']),
            "status"            => 1,
            "update_by_users"   => $this->session->userdata("id"),
        );
        $id_news_content = $this->global_models->update("d_forum_topic", array("id_forum_topic" => $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            "title"             => $pst['title'],
            "nicename"          => $this->global_models->nicename($pst['title']),
            "note"              => str_replace("../../../", base_url(), $pst['note']),
            "status"            => 1,
            "create_by_users"   => $this->session->userdata("id"),
            "create_date"       => date("Y-m-d H:i:s")
        );
        $id_news_content = $this->global_models->insert("d_forum_topic", $kirim);
      }
      if($id_news_content)
        redirect("forum/index/1/Berhasil");
      else
        redirect("forum/index/2/Gagal");
    }
    else{
      $data = $this->global_models->get("d_forum_topic", array("nicename" => $nicename));
      $this->template->build("add-topic",
        array(
              'url'         => base_url()."themes/srabon/",
              'title_table' => lang('eforum')." ".lang("topic"),
              'data'        => $data,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-topic");
    }
  }
	public function add_forum($id_forum_topic, $nicename){
    if(!$nicename)
      redirect("forum/add-forum/{$id_forum_topic}/create");
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"             => $pst['title'],
            "nicename"          => $this->global_models->nicename($pst['title']),
            "note"              => str_replace("../../../../", base_url(), $pst['note']),
            "status"            => 1,
            "update_by_users"   => $this->session->userdata("id"),
        );
        $id_news_content = $this->global_models->update("d_forum_content", array("id_forum_content" => $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            "id_forum_topic"    => $pst['id_forum_topic'],
            "title"             => $pst['title'],
            "nicename"          => $this->global_models->nicename($pst['title']),
            "note"              => str_replace("../../../../", base_url(), $pst['note']),
            "status"            => 1,
            "create_by_users"   => $this->session->userdata("id"),
            "create_date"       => date("Y-m-d H:i:s")
        );
        $id_news_content = $this->global_models->insert("d_forum_content", $kirim);
      }
      redirect("forum/views/topic/".$this->global_models->get_field("d_forum_topic", "nicename", array("id_forum_topic" => $pst['id_forum_topic'])));
    }
    else{
      $data = $this->global_models->get("d_forum_content", array("nicename" => $nicename));
      $this->template->build("add-forum",
        array(
              'url'         => base_url()."themes/srabon/",
              'title_table' => lang('eforum'),
              'data'        => $data,
              'id_forum_topic' => $id_forum_topic
            ));
      $this->template
        ->set_layout('default')
        ->build("add-forum");
    }
	}
  public function ajax_topic($total = 0, $start = 0){
    $data = $this->global_models->get_query("
      SELECT A.*, B.bio_first_name, B.bio_last_name, C.title as unit_kerja
      FROM d_forum_topic AS A
      LEFT JOIN m_users AS B ON A.create_by_users = B.id_users
      LEFT JOIN m_users_unit_kerja AS C ON B.id_users_unit_kerja = C.id_users_unit_kerja
      WHERE A.status = 1
      AND (LOWER(A.title) LIKE '%".strtolower($this->session->userdata("key_news"))."%'
      OR LOWER(A.note) LIKE '%".strtolower($this->session->userdata("key_news"))."%')
      ORDER BY A.create_date ASC LIMIT {$start},10
      ");
    
    foreach ($data as $key => $value) {
      
      print "
      <tr class='ajax-topic'>
        <td><a href='".site_url("forum/views/topic/".str_replace("_", "-", $value->nicename))."'>{$value->title}</a></td>
        <td>
          <a href='#'><span class='color-icons user_co'></span><span class='btn_link'>{$value->bio_first_name} {$value->bio_last_name}</span></a><br />
          <span class='color-icons briefcase_co'></span><span class='btn_link'>{$value->unit_kerja}</span><br />
          <span class='color-icons calendar_1_co'></span><span class='btn_link'>{$value->create_date}</span>
        </td>
        <td>
          <a href='".site_url("forum/views/topic/".str_replace("_", "-", $value->nicename))."'><span class='color-icons magnifier_co'></span>View</a><br />
          <a href='".site_url("forum/add-topic/".str_replace("_", "-", $value->nicename))."'><span class='color-icons page_white_edit_co'></span>Edit</a><br />
          <a href='".site_url("forum/delete-topic/".str_replace("_", "-", $value->nicename))."'><span class='color-icons cross_co'></span>Delete</a>
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_topic";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-topic'>
      <td colspan='3'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  function delete_topic($nicename){
    $this->global_models->delete("d_forum_topic", array("nicename" => $nicename));
    redirect("forum");
  }
  function delete_forum($nicename, $n_topic){
    $this->global_models->delete("d_forum_content", array("nicename" => $nicename));
    redirect("forum/views/topic/".$n_topic);
  }
  function cari_news(){
    $cari = array(
        "key_news"    => $this->input->post("no")
    );
    $this->session->set_userdata($cari);
    die;
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */